package com.itheima.d2_udp.no2;

import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.util.Scanner;

//需求：使用UDP通信实现多发消息
public class SendDemo02 {
    public static void main(String[] args) throws Exception {
        //1.要发送的数据需要键盘录入，创建Scanner对象
        Scanner sc = new Scanner(System.in);

        //2.创建DatagramSocket对象，用来发送数据
        DatagramSocket socket = new DatagramSocket();


        //3.不断的发，使用while死循环
        while (true) {
            System.out.println("请输入：");
            String msg = sc.next();

            //4.判断输入的字符串是否是"exit"
            if ("exit".equals(msg)) {
                //成立，结束循环
                break;
            }

            //5.如果代码执行到这，说明输入的不是"exit"，就把数据打包，然后发送
            byte[] bys = msg.getBytes();
            DatagramPacket packet = new DatagramPacket(bys, bys.length, InetAddress.getByName("192.168.68.255"),8585);
            socket.send(packet);
        }

        //6.释放资源
        socket.close();
    }
}
